package com.example.bailin.giftsay.guide.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseActivity;
import com.example.bailin.giftsay.entity.LmSkipEntity;
import com.example.bailin.giftsay.sort.adapter.LmTwoLvAdapter;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

/**
 * Created by bailin on 16/8/29.
 */
public class NickNameActivity extends BaseActivity implements AbsListView.OnScrollListener {

    private ListView lv;
    private TextView tvTitle;
    private RelativeLayout rlTitle;
    private ImageView ivName;

    @Override
    protected int setLayout() {
        return R.layout.fragment_selection;
    }

    @Override
    protected void initView() {
        lv = bindView(R.id.lv_selection);
        rlTitle = bindView(R.id.rl_selection_title);
        tvTitle = bindView(R.id.tv_item_selection_title);
        ImageView ivTitle = bindView(R.id.iv_selection_return);
        lv.setOnScrollListener(this);
        ivTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String imgUrl = intent.getStringExtra("imgUrl");
        int id = intent.getIntExtra("id", 0);
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_nickname, null);
        ivName = (ImageView) view.findViewById(R.id.iv_nickname);
        ImageView ivZname = (ImageView) view.findViewById(R.id.iv_zhong_nickname);
        TextView tvName = (TextView) view.findViewById(R.id.tv_nickname);
        tool().blurryImg(imgUrl, ivName);
        tool().getRoundImg(imgUrl, ivZname);
        tvName.setText(name);
        lv.addHeaderView(view);

//        ivName.setAlpha(50); // 图片透明
        tool().getData(UrlProvide.NICK_NAME + id + UrlProvide.NICK_NAME1, LmSkipEntity.class, new NetTool.NetInterface<LmSkipEntity>() {
            @Override
            public void onSuccess(LmSkipEntity lmSkipEntity) {
                LmTwoLvAdapter adapter = new LmTwoLvAdapter();
                adapter.setLmSkipEntity(lmSkipEntity);
                adapter.setIndex(911);
                lv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        float scale = (float) getScrolly() / 200;
        float alpha = (255 * scale);
        if (view.getChildAt(0) != null) {
            getScrolly();
            if (getScrolly() <= 0) {
                tvTitle.setText(null);
                rlTitle.setBackgroundColor(Color.argb( 0, 244, 62, 94));//AGB由相关工具获得，或者美工提供
            } else if (getScrolly() > 0 && getScrolly() <= 200) {
                rlTitle.setBackgroundColor(Color.argb((int) alpha, 244, 62, 94));
            } else {
                rlTitle.setBackgroundColor(Color.argb( 255, 244, 62, 94));


                if (getScrolly() > 200 && getScrolly() <= 400) {
                    tvTitle.setText("作者主页");
                    tvTitle.setTextColor(Color.argb((int)alpha, 255, 255, 255));
                }
                if (getScrolly() > 400) {
                    tvTitle.setText("作者主页");
                    tvTitle.setTextColor(Color.argb(255, 255, 255, 255));
                }

            }

        }
    }

    public int getScrolly() {
        View view = lv.getChildAt(0);
        if (view == null) {
            return 0;
        }
        int firstVisiblePosition = lv.getFirstVisiblePosition();
        int top = view.getTop();
        return -top + firstVisiblePosition * ivName.getHeight();
    }
}
